var gLiveListObj;
var LiveList = function(opt){
	this.typeListDiv = opt.typeListDiv?opt.typeListDiv:'typeListBox';
	this.dataListDiv = opt.dataListDiv?opt.dataListDiv:'dataListBox';
	this.typeTopDiv = opt.typeTopDiv?opt.typeTopDiv:'typeTopDiv';
	this.typeBottomDiv = opt.typeBottomDiv?opt.typeBottomDiv:'typeBottomDiv';
	this.disTypeNum = opt.disTypeNum?opt.disTypeNum:8;				//导航显示个数
	this.typeItemHeight = opt.typeItemHeight?opt.typeItemHeight:55;	//分类高度
	this.defaultTypeColor = opt.defaultTypeColor?defaultTypeColor.defaultTypeColor:'#fff';
	this.selectTypeColor = opt.selectTypeColor?selectTypeColor.defaultTypeColor:'#ff0';
	this.disNum = opt.disNum?opt.disNum:12;						//显示个数
	this.splitHeight = opt.splitHeight?opt.splitHeight:432;		//分割条高度
	this.itemHeight = opt.itemHeight?opt.itemHeight:36;			//每一项高度
	this.splitBarDiv = opt.splitBarDiv?opt.splitBarDiv:'pSplitBar';		//分割条ID
	
	this.onfocusType = opt.onfocusType?opt.onfocusType:false;		//焦点在导航上
	this.onfocusItem = opt.onfocusItem?opt.onfocusItem:false;		//焦点在item上
	this.onclickItem = opt.onclickItem?opt.onclickItem:false;		//点击事件
	
	if(opt.formatTypeHtml){
		this.formatTypeHtml = opt.formatTypeHtml;
	}
	if(opt.initDataList){
		this.initDataList = opt.initDataList;
	}
	if(opt.formatItemHtml){
		this.formatItemHtml = opt.formatItemHtml;
	}
	this.typeList = opt.typeList?opt.typeList:null;
	this.curTypeId = 0;
	this.focusTypeId = 0;
	this.curTopTypeId = 0;
	
	gLiveListObj = this;
}

LiveList.prototype = {
	setFocusItem:function(){
		if(this.curTypeId > 0 && this.typeList){
			var type = this.typeList[this.curTypeId - 1];
			if(type){
				var a = document.getElementById('item-a-'+type.curItemId);
				if(a){
					a.focus();
				}
			}
		}
	},
	setFocusType:function(){
		if(this.curTypeId > 0 && this.typeList){
			var a = document.getElementById('type-a-'+this.focusTypeId);
			if(a){
				a.focus();
			}
			
		}
	},
	setTypeList:function(typeList,onFocus){
		this.typeList = typeList?typeList:new Array();
		this.curTypeId = -1;
		this.focusTypeId = -1;
		this.curTopTypeId = 0;
		this.refreshTypeContent();
		if(onFocus){
			var a = document.getElementById('type-a-1');
			if(a){
				a.focus();
				a.click();
			}
		}
	},
	refreshTypeContent:function(){
		var typeListBox = document.getElementById(this.typeListDiv);
		typeListBox.style.top = '0px';
		
		var typeBottom = document.getElementById(this.typeBottomDiv);
		var typeTop = document.getElementById(this.typeTopDiv);
		
		typeBottom.style.display ='none';
		typeTop.style.display = 'none';
		/*
		if(this.typeList.length > this.disTypeNum){
			typeBottom.style.display ='block';
			typeTop.style.display = 'none';
		}else{
			typeBottom.style.display ='none';
			typeTop.style.display = 'none';
		}
		*/
		var con = '';
		if(this.typeList){
			for(var i=0;i<this.typeList.length;i++){
				con +='<div class="_typeItem">'+this.formatTypeHtml(typeList[i],i+1)+'</div>';
			}
		}
		typeListBox.innerHTML = con;
		var aList = typeListBox.getElementsByTagName('a');
		var a;
		for(var i = 0;i<aList.length;i++){
			a = aList[i];
			a.onfocus = this.focusType;
			a.onblur = this.blurType;
			a.onclick = this.clickType;
		}
	},
	formatTypeHtml:function(type,id){
		var con = '';
		con +='<div class="_tIcon"><img src="images/7.png"/></div>';
		con +='<div class="_tText"><a href="#" tar="'+id+'" id="type-a-'+id+'">'+type.name+'</a></div>';
		
		return con;
	},
	blurType:function(event){
		var a = event.currentTarget;
		var par = a.parentNode.parentNode;
		par.className = '_typeItem';
	},
	focusType:function(event){
		var a = event.currentTarget;
		var par = a.parentNode.parentNode;
		par.className = '_curTypeItem';
		
		var tar = parseInt(a.getAttribute('tar'));
		gLiveListObj.focusTypeId = tar;
		gLiveListObj.changeTypePosition(tar);
		if(gLiveListObj.onfocusType){
			var type = gLiveListObj.typeList[tar - 1];
			gLiveListObj.onfocusType(type,tar,gLiveListObj);
		}
		
	},
	clickType:function(event){
		var a = event.currentTarget;
		var tar = parseInt(a.getAttribute('tar'));
		var type = gLiveListObj.typeList[tar - 1]; 
		if(tar!=gLiveListObj.curTypeId){
			var oldA = document.getElementById('type-a-'+gLiveListObj.curTypeId);
			if(oldA){
				oldPar = oldA.parentNode;
				oldPar.style.color = gLiveListObj.defaultTypeColor;
			}		
			gLiveListObj.curTypeId = tar;
			var a = document.getElementById('type-a-'+gLiveListObj.curTypeId);
			var par = a.parentNode;
			par.style.color = gLiveListObj.selectTypeColor;
			
			gLiveListObj.refreshListContent(type,tar);
		}else{
			var a = document.getElementById('item-a-'+type.curItemId);
			if(a){
				a.focus();
			}
		}
	},
	changeTypePosition:function(tar){
		if(this.typeList.length > this.disTypeNum){
			var typeBottom = document.getElementById(this.typeBottomDiv);
			var typeTop = document.getElementById(this.typeTopDiv);
			var typeListBox = document.getElementById(this.typeListDiv);
			
			if(tar - this.curTopTypeId == this.disTypeNum + 1){			//向下
				this.curTopTypeId ++;
				typeListBox.style.top = (0 - this.curTopTypeId) * this.typeItemHeight;
			}else if(tar == this.curTopTypeId){
				this.curTopTypeId -- ;
				typeListBox.style.top = (0 - this.curTopTypeId) * this.typeItemHeight;
			}
			
			if(tar - this.curTopTypeId == gLiveListObj.disTypeNum && tar!=gLiveListObj.typeList.length){
				typeBottom.style.display = 'block';
			}else{
				typeBottom.style.display = 'none';
			}
			
			if(tar - this.curTopTypeId == 1 && tar!=1){
				typeTop.style.display = 'block';
			}else{
				typeTop.style.display = 'none';
			}
			
		}
	},
	refreshListContent:function(type,n){
		if(!type.dataList){
			this.initDataList(type,n,this);
		}else{
			if(!type.hasOwnProperty("showSplit")){
				this.formatTypeItem(type);
			}
			this.initListContent(type);
		}
	},
	initDataList:function(type,n,obj){
		type.dataList = new Array();
		this.refreshListContent(type,n);
	},
	formatTypeItem:function(type){
		var length = type.dataList.length;
		if(length > this.disNum){
			type.showSplit = true;
			if(length< 2*this.disNum){
				type.splitAddValue = this.itemHeight;
			}else{
				type.splitAddValue = (this.splitHeight - this.itemHeight)/(length - this.disNum);
			}
			type.splitBarHeight = this.splitHeight - (length - this.disNum)*type.splitAddValue;
		}else{
			type.showSplit = false;
		}
		type.curItemId = 1;
		type.topItemId = 0;
	},
	initListContent:function(type){
		var dataList = type.dataList;
		var start = type.topItemId;
		var end = start + this.disNum + 2;
		var length = dataList.length; 
		
		var j,data;
		var con = '';
		for(var i = start;i<end;i++){
			j = i;
			
			if(j<1 || j>length){
				con+='<div class="_item0">';
				con+='&nbsp;';
			}else{
				con+='<div class="_item">';
				data = dataList[j-1];
				con+=this.formatItemHtml(data,j);
			}
			con+='</div>';
		}
		
		var listBox = document.getElementById(this.dataListDiv);
		listBox.innerHTML = con;
		
		this.showSplit(type);
		
		var aList = listBox.getElementsByTagName('a');
		var a;
		for(var i = 0;i<aList.length;i++){
			a = aList[i];
			a.onblur = this.blurItem;
			a.onclick = this.clickItem;
			a.onfocus = this.focusItem;
		}
	},
	formatItemHtml:function(data,j){
		var con = '';
		var id = j>9?j+'':'0'+j;
		con+='<div class="_num"><a href="#" tar="'+j+'" id="item-a-'+j+'">'+id+'</a></div><div class="_name">'+data.name+'</div>';
		return con;
	},
	showSplit:function(type){
		var splitBar = document.getElementById(this.splitBarDiv);
		var par = splitBar.parentNode;
		if(type.showSplit){
			par.style.display ='block';
			splitBar.style.height =type.splitBarHeight+'px';
			this.changeSplit(type);
			return true;
		}else{
			par.style.display ='none';	
			return false;
		}
	},
	changeSplit:function(type){
		if (type.showSplit) {
			var topItemId = type.topItemId;
			var max = type.dataList.length - this.disNum;
			if(topItemId>max){
				topItemId = max;
			}
					
			var top = topItemId * type.splitAddValue;
			var splitBar = document.getElementById(this.splitBarDiv);
			splitBar.style.top = top+'px';
		}
	},
	blurItem:function(event){
		var a = event.currentTarget;
		var par = a.parentNode.parentNode;
		par.className = '_item';
	},
	focusItem:function(event){
		var a = event.currentTarget;
		var par = a.parentNode.parentNode;
		par.className = '_curItem';
		
		var _this = gLiveListObj;
		var tar = parseInt(a.getAttribute('tar'));
		var type = _this.typeList[_this.curTypeId - 1];
		if(type.showSplit){
			_this.moveItem(type,tar);
		}else{
			type.curItemId = tar;
		}
		if(_this.onfocusItem){
			var data = type.dataList[tar - 1];
			_this.onfocusItem(data,tar,type,_this);
		}
	},
	clickItem:function(event){
		var a = event.currentTarget;
		var _this = gLiveListObj;
		var tar = parseInt(a.getAttribute('tar'));
		var type = _this.typeList[_this.curTypeId - 1];
		var data = type.dataList[tar - 1];
		if(_this.onclickItem){
			_this.onclickItem(_this.curTypeId,tar,type,data);
		}
	},
	moveItem:function(type,tar){
		if (tar == type.curItemId) { this.changeSplit(type);return ;}
	
		var listBox = document.getElementById(this.dataListDiv);
		var childNodes = listBox.childNodes;
		var dataList = type.dataList;
		var totalNum = dataList.length;
		
		if (tar > type.curItemId) { //向下
			type.curItemId = tar;
			
			var bo = 0;
			if(tar - type.topItemId == this.disNum && tar != totalNum){		//当焦点不是在最上面的时候
				bo = 1;
			}else if(tar == type.topItemId + this.disNum + 1){			//当焦点是第一个时
				bo = 2;
			}
			
			if (bo>0) { //
				
				if( childNodes.length  >= this.disNum + 2 ){
					listBox.removeChild(childNodes[0]);		//删除第一个节点
				}
				
				var dis = 2;
				if(bo==2){dis =1;};
				
				
				var nId = tar+dis;
				
				var div = document.createElement('div');
				
				
				if(nId>totalNum){
					div.setAttribute('class','_item0');
					div.innerHTML = '&nbsp;';
					listBox.appendChild(div);
				}else{
					div.setAttribute('class','_item');
					var data = dataList[nId - 1];
					div.innerHTML = this.formatItemHtml(data,nId);
					listBox.appendChild(div);
					
					var a = document.getElementById('item-a-'+nId);
					a.onfocus = this.focusItem;
					a.onblur = this.blurItem;
					a.onclick = this.clickItem;
				}
				type.topItemId++;
				this.changeSplit(type);
			}
		}else if(tar<type.curItemId){ //向上
			type.curItemId = tar;
			
			var bo = 0;
			if(tar - type.topItemId == 1 && tar!= 1){		//当焦点不是在最上面的时候
				bo = 1;
			}else if(tar == type.topItemId){			//当焦点是第一个时
				bo = 2;
			}
			
			alert('bo:::'+bo);
			if(bo>0){	
				if( childNodes.length  >= this.disNum + 2){
					listBox.removeChild(childNodes[childNodes.length - 1]);		//删除最后一个节点
				}
				
				var dis = 2;
				if(bo==2){dis =1;};
				
				var pId = tar-dis;
				var div = document.createElement('div');
				
				if(pId<1){
					div.setAttribute('class','_item0');
					div.innerHTML = '&nbsp;';
					listBox.insertBefore(div,childNodes[0]);
				}else{
					div.setAttribute('class','_item');
					var data = dataList[pId - 1];
					div.innerHTML = this.formatItemHtml(data,pId);
					listBox.insertBefore(div,childNodes[0]);
					
					var a = document.getElementById('item-a-'+pId);
					a.onfocus = this.focusItem;
					a.onblur = this.blurItem;
					a.onclick = this.clickItem;
				}
				
				
				
				type.topItemId--;
				this.changeSplit(type);
			}
		}
	},
	pageDown:function(justForPage){
		if(this.curTypeId <1 || !this.typeList || this.typeList.length == 0){
			return ;
		}
		var type = this.typeList[this.curTypeId - 1];
		var dataList = type.dataList;
		var nextPage = type.topItemId + this.disNum;
		if(nextPage <dataList.length){
			var a = document.getElementById(justForPage);
			a.focus();
			
			type.topItemId = nextPage;
			type.curItemId = nextPage + 1;
			this.initListContent(type);
			
			var na = document.getElementById('item-a-'+(nextPage+1));
			na.focus();
		}
	},
	pageUp:function(jsutForPage){
		if(this.curTypeId <1 || !this.typeList || this.typeList.length == 0){
			return ;
		}
		var type = this.typeList[this.curTypeId - 1];
		var dataList = type.dataList;
		if(type.topItemId > 0){
			var a = document.getElementById(jsutForPage);
			if(a){
				a.focus();
			}
			
			var prevPage = type.topItemId - this.disNum;
			var oldCurItemId = type.curItemId;
			if(prevPage < 0 ){
				prevPage = 0;
			}
			
			type.topItemId = prevPage;
			if(prevPage+this.disNum >= oldCurItemId){
				type.curItemId = oldCurItemId;
			}else{
				type.curItemId = prevPage+this.disNum;
			}
			this.initListContent(type);
			var na = document.getElementById('item-a-'+type.curItemId);
			na.focus();
		}
	},
}